#include"../include/p.h"
#include<stdio.h>
//编写子函数 实现下载文件
int c_Download(int tcp_socket,char *filename)
{
	char buf[100]={0};
	//接收文件大小
	int filesize=0;
	recv(tcp_socket,buf,sizeof(buf),0);

	printf("%s",buf);
	//将char转为int
	filesize=atoi(buf);
	if(filesize==0)
	{
		printf("文件为空！\n");

	}
	memset(buf,0,sizeof(buf));
	send(tcp_socket,"ok",strlen("ok"),0);
	int fw=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0664);
	//打开文件写入
	if(fw<0)
	{
		perror("open error");
		return -1;
	}
	int count = 0 ;
	while(count<filesize)
	{
		memset(buf,0,sizeof(buf));
		int n=recv(tcp_socket,buf,sizeof(buf),0);
		write(fw,buf,n);
		count+=n;
	}
	printf("文件下载成功！\n");
	close(fw);
	return 0;
}


